import Taro from '@tarojs/taro'
import { CONSULTATION_TYPE } from '@CONSTANTS/common'
import Navigate from '@NAVIGATION/navigateManager'
import { CONSULTATION_STATUS_ENUMS } from '@CONSTANTS/consultation'
import i18n from '@I18N'
import api from '@API'

export const quickInquiry = async (pId, doctor, language, dispatchUpdateConsultation) => {
  try {
    const { InquiryRate, DisplayName, UserId } = doctor || {}
    const { Existing, Order, Consultation } = await api.getExistingConsultation({ t: CONSULTATION_TYPE.PICTURE_TEXT, dId: UserId, pId })
    if (Existing) {
      Taro.showModal({
        content: i18n.patient.patient_tips2(),
        confirmColor: '#47C690',
        confirmText: '查看',
        cancelColor: '#595959',
        success: res => {
          if (res.confirm) {
            Consultation?.Status === CONSULTATION_STATUS_ENUMS.InProgress
              ? Navigate.chatRoomPage({ ConsultationId: Consultation?.p8Id })
              : Navigate.orderDetail({ orderId: Order?.p8Id })
          }
        }
      })
      return
    }
    dispatchUpdateConsultation({
      inquiryRate: InquiryRate,
      doctorId: UserId,
      patientId: pId,
      doctorName: DisplayName?.[language]
    })
    Navigate.payment({ type: CONSULTATION_TYPE.PICTURE_TEXT })
  } catch (error) {
    Taro.showToast({
      title: error.message || error,
      icon: 'none'
    })
  }
}
